package com.apps.diet.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ws.soap.client.SoapFaultClientException;

import com.apps.diet.oxm.DeleteNutriFactRequest;
import com.apps.diet.oxm.DeleteNutriFactResponse;
import com.apps.diet.oxm.DietPort;
import com.apps.diet.oxm.EditMealsRequest;
import com.apps.diet.oxm.EditMealsResponse;
import com.apps.diet.oxm.EditNutriFactRequest;
import com.apps.diet.oxm.EditNutriFactResponse;
import com.apps.diet.oxm.GetNutriFactRequest;
import com.apps.diet.oxm.GetNutriFactResponse;
import com.apps.diet.oxm.Meals;
import com.apps.diet.oxm.NutriFacts;

/**
 * Handles and retrieves the subscription request
 */
@Controller
public class DietController {

	protected static Logger logger = Logger.getLogger(DietController.class);

	@Resource(name="dietJaxProxyService")
	private DietPort dietJaxProxyService;
	
	/**
	 * Handles the get diet request
	 */
	@RequestMapping(value = "/diet/getnutrifacts.view", method = RequestMethod.GET)
	@ResponseBody
	public GetNutriFactResponse getNutriFacts(@RequestParam("itemName") String itemName) {
		GetNutriFactRequest request = new GetNutriFactRequest();
		GetNutriFactResponse response = null;
		
		request.setItemName(itemName);
		
		if (itemName==null){
			response = new GetNutriFactResponse();
			response.setCode("FAILURE");
			response.setTotalRecord(0);
			return response;
		}
		
		try {
			response = dietJaxProxyService.getNutriFact(request);
			logger.debug(response.getCode());
		} catch (SoapFaultClientException sfce) {
			logger.error("We sent an invalid message", sfce);
		} catch (Exception e) {
			logger.error("Unexpected exception", e);
		}

		return response;
	}
	
	/**
	 * Handles the edit nutrifact request
	 */
	@RequestMapping(value = "/diet/editNutriFacts.do", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public EditNutriFactResponse editNutriFacts(@RequestBody NutriFacts nutriFact, @RequestParam String reqType) {
		EditNutriFactRequest request = new EditNutriFactRequest();
		EditNutriFactResponse response = null;

		if (reqType.equals("ADD")){
			request.setEditType("ADD");
		}else if (reqType.equals("UPDATE")){
			request.setEditType("UPDATE");
		}
		request.setNutriFacts(nutriFact);

		try {
			response = dietJaxProxyService.editNutriFact(request);
			logger.debug(response.getCode());
		} catch (SoapFaultClientException sfce) {
			logger.error("We sent an invalid message", sfce);
		} catch (Exception e) {
			logger.error("Unexpected exception", e);
		}

		return response;
	}

	/**
	 * Handles the add nutrifact request
	 */
	@RequestMapping(value = "diet/addNutriFacts.do", method = RequestMethod.GET)
	@ResponseBody
	public EditNutriFactResponse addNutriFacts(@RequestBody NutriFacts nutriFact) {
		EditNutriFactRequest request = new EditNutriFactRequest();
		EditNutriFactResponse response = null;

		request.setEditType("ADD");
		request.setNutriFacts(nutriFact);
		try {
			response = dietJaxProxyService.editNutriFact(request);

			logger.debug(response.getCode());
		} catch (SoapFaultClientException sfce) {
			logger.error("We sent an invalid message", sfce);
		} catch (Exception e) {
			logger.error("Unexpected exception", e);
		}

		return response;
	}

	/**
	 * Handles the delete nutrifact request
	 */
	@RequestMapping(value = "/diet/removeNutriFacts.do", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public DeleteNutriFactResponse deleteNutriFacts(@RequestParam("nutriFactId") String nutriFactId) {
		DeleteNutriFactRequest request = new DeleteNutriFactRequest();
		DeleteNutriFactResponse response = null;

		request.setNutriFactsId(Integer.parseInt(nutriFactId));
		try {
			response = dietJaxProxyService.deleteNutriFact(request);
			logger.debug(response.getCode());
		} catch (SoapFaultClientException sfce) {
			logger.error("We sent an invalid message", sfce);
		} catch (Exception e) {
			logger.error("Unexpected exception", e);
		}

		return response;
	}
	
	/**
	 * Handles the add meals request
	 */
	@RequestMapping(value = "diet/addMeals.do", method = RequestMethod.POST)
	@ResponseBody
	public EditMealsResponse addMeals(@RequestBody Meals meal) {
		logger.debug("Inside EditMealsResponse");
		
		EditMealsRequest request = new EditMealsRequest();
		EditMealsResponse response = null;

		request.setEditType("ADD");
		request.setMeals(meal);
		try {
			response = dietJaxProxyService.editMeals(request);

			logger.debug(response.getCode());
		} catch (SoapFaultClientException sfce) {
			logger.error("We sent an invalid message", sfce);
		} catch (Exception e) {
			logger.error("Unexpected exception", e);
		}

		return response;
	}
	
	/**
	 * Handles the add meal request
	 */
	@RequestMapping(value = "diet/editMeals.do", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public EditMealsResponse addNutriFacts(@RequestBody Meals meal) {
		EditMealsRequest request = new EditMealsRequest();
		EditMealsResponse response = null;

		request.setEditType("UPDATE");
		request.setMeals(meal);
		try {
			response = dietJaxProxyService.editMeals(request);

			logger.debug(response.getCode());
		} catch (SoapFaultClientException sfce) {
			logger.error("We sent an invalid message", sfce);
		} catch (Exception e) {
			logger.error("Unexpected exception", e);
		}

		return response;
	}

}